package com.yikome.shop.controller;

import java.util.Calendar;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.yikome.order.entity.Order;
import com.yikome.order.entity.OrderAddress;
import com.yikome.order.entity.OrderExt;
import com.yikome.order.entity.OrderProduct;
import com.yikome.order.enums.OrderStatus;
import com.yikome.order.repository.OrderAddressRepository;
import com.yikome.order.repository.OrderExtRepository;
import com.yikome.order.repository.OrderProductRepository;
import com.yikome.order.repository.OrderRepository;
import com.yikome.utils.BeanUtils;
import com.yikome.utils.JSONUtils;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.StrUtil;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@RestController
@RequestMapping("/test")
public class TestController {
	
	@Autowired
	private OrderRepository orderRepository;
	
	@Autowired
	private OrderExtRepository orderExtRepository;
	
	@Autowired
	private OrderAddressRepository orderAddressRepository;
	
	@Autowired
	private OrderProductRepository orderProductRepository;
	
	@Autowired
	private NamedParameterJdbcTemplate jdbcTemplate;
	
	@GetMapping("async/{date}/{minHour}/{maxHour}")
	@Transactional
	public Boolean testCreate(@PathVariable("date") String date, @PathVariable("minHour") Integer minHour, @PathVariable("maxHour") Integer maxHour) {
		
		List<Order> orders = jdbcTemplate.query("SELECT * FROM o_order WHERE o_order.yn = 1 and o_order.created_at > :date", MapUtil.of("date", date), new BeanPropertyRowMapper<>(Order.class));
		
		for (Order order : orders) {
			
			log.info("原始订单数据: {}" , JSONUtils.toJsonString(order));
			
			Order newOrder = new Order();
			BeanUtils.copyProperties(order, newOrder, "id" , "remark" , "status");
			newOrder.setCode(StrUtil.subWithLength(order.getCode(), 0, 21) + RandomUtil.randomString(1) + RandomUtil.randomInt(1, 9));
			newOrder.setStatus(OrderStatus.ORDER_DELIVERY.getStatus());
			
			Calendar createdAt = Calendar.getInstance();
			createdAt.setTime(order.getCreatedAt());
			createdAt.set(Calendar.HOUR, createdAt.get(Calendar.HOUR) + RandomUtil.randomInt(minHour, maxHour));
			newOrder.setCreatedAt(createdAt.getTime());
			log.info("重构订单数据: {}" , JSONUtils.toJsonString(newOrder));
			
			orderRepository.save(newOrder);
			
			OrderExt orderExt = orderExtRepository.findByOrderCode(order.getCode());
			OrderExt newOrderExt = new OrderExt();
			BeanUtils.copyProperties(orderExt, newOrderExt, "id");
			newOrderExt.setOrderCode(newOrder.getCode());
			orderExtRepository.save(newOrderExt);
			
			OrderAddress orderAddress = orderAddressRepository.findOrderAddress(order.getCode());
			OrderAddress newOrderAddress = new OrderAddress();
			BeanUtils.copyProperties(orderAddress, newOrderAddress, "id");
			newOrderAddress.setOrderCode(newOrder.getCode());
			orderAddressRepository.save(newOrderAddress);
			
			List<OrderProduct> orderProducts = orderProductRepository.findOrderProducts(order.getCode());
			for (OrderProduct orderProduct : orderProducts) {
				OrderProduct newOrderProduct = new OrderProduct();
				BeanUtils.copyProperties(orderProduct, newOrderProduct, "id");
				newOrderProduct.setOrderCode(newOrder.getCode());
				orderProductRepository.save(newOrderProduct);
			}
		}
		
		return Boolean.TRUE;
	}
}
